home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Science / µSim 1.0b5 folder / source / TrackThumb.c < prev    next >
Encoding:
Text File  |  1994-07-08  |  1.9 KB  |  76 lines  |  [TEXT/MMCC]

  1. /*
  2. Copyright © 1993,1994 by Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware: you can copy, exchange, modify this
  5. code as you wish. You may include this code in any kind of application: freeware,
  6. shareware, or commercial, provided that full credits are given.
  7. You may not sell or distribute this code for profit.
  8. */
  9.  
  10. //#pragma load "MacDump"
  11.  
  12. #include    "TrackThumb.h"
  13.  
  14. //#pragma segment Main
  15.  
  16. /*
  17.     TrackThumb must be called as follows:
  18.     partCode = TrackThumb(whichControl, localPoint, actionProc);
  19.  
  20.     actionProc must be declared as follows:
  21.     pascal void actionProc(ControlHandle control, short value);
  22. */
  23.  
  24. short TrackThumb(ControlHandle control, Point start, void (*action)(ControlHandle, short))
  25. {
  26. enum {
  27. kTooDistant = 23
  28. };
  29.  
  30. EventRecord    dummyEventRec;
  31. Rect    rct;
  32. GrafPtr    savePort;
  33. Point    pos;
  34. register long    val, new;
  35. register short    min, max, old, widthOrheight;
  36.  
  37. GetPort(&savePort);
  38. SetPort((*control)->contrlOwner);
  39. min    = GetCtlMin(control);        /* min value */
  40. max    = GetCtlMax(control);        /* max value */
  41. val    = GetCtlValue(control);        /* cur value */
  42. rct    = (*control)->contrlRect;    /* control's rectangle */
  43.  
  44. widthOrheight = rct.right - rct.left;
  45. old = -32768;
  46. do {
  47.     GetMouse(&pos);
  48.  
  49.     if (pos.v < rct.top + widthOrheight)
  50.         pos.v = rct.top + widthOrheight;
  51.     else if (pos.v > rct.bottom - widthOrheight)
  52.         pos.v = rct.bottom - widthOrheight;
  53.     new = val + (long)(max - min) * (pos.v - start.v) /
  54.                                 (rct.bottom - rct.top - (widthOrheight * 3));
  55.     if (new < min)
  56.         new = min;
  57.     if (new > max)
  58.         new = max;
  59.     if ((pos.h - rct.right > kTooDistant) || (rct.left - pos.h > kTooDistant))
  60.         new = val;
  61.  
  62.     if (new != old) {
  63.         SetCtlValue(control, new);
  64.         action(control, old = GetCtlValue(control));
  65.         }
  66.     else {    /* give some time to other applications */
  67.         SystemTask();
  68.         (void)EventAvail(everyEvent, &dummyEventRec);
  69.         }
  70.     }
  71. while(StillDown());
  72. SetPort(savePort);
  73. return inThumb;
  74. }
  75.  
  76.